# -*- coding: UTF-8 -*-
import os
import os.path
import sys
current_dir = os.path.abspath(os.path.dirname(__file__))
sys.path.append(current_dir)
sys.path.append("..")

from pyh import *
from case import *
from cash import *
import time


def tableDsprCss(table = None,width='90%'):
    table.attributes['cellSpacing'] = 1;  
    table.attributes['cellPadding'] = 1;
    table.attributes['border'] = 1;  
    table.attributes['borderColor'] = '#666666';  
    table.attributes['width'] = width;

def tablecss(table = None,width='90%'):
    table.attributes['cellSpacing'] = 1;  
    table.attributes['cellPadding'] = 1;
    table.attributes['border'] = 1;  
    table.attributes['borderColor'] = '#666666';  
    table.attributes['width'] = width;        
def tr_title_css(tr = None):
    tr.attributes['bgcolor'] = '#CCCC00'; 

def genCaseReport(fileName, report_case):
    assert isinstance(report_case, case)
    report_case.run(None)
    table_head_color_green = "#66B3FF"
    table_head_color_red = "#FF0033"
    page = PyH('Test Case (%s) Report'%report_case.getDescriptionAttribute(case.name_str))
    page << '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'
    title_div = div(b('Test Case \"%s\" Report'%report_case.getDescriptionAttribute(case.name_str)), id='myCSSid1')
    page << title_div
    curr_time = time.strftime("%Y-%m-%d %X",time.localtime())
    case_information_div = div()
    case_description_div = div()
    
    case_description_div << span("Generate Data: %s"% curr_time) << br()
    for key in case.description_key:
        case_description_div << span("%s: %s"%(key,report_case.getDescriptionAttribute(key))) << br()
    page << case_description_div

    for key in case.information_key:
        case_information_div << span("%s: %s"%(key,report_case.getInformationAttribute(key))) << br()
    page << case_information_div 
        
    
    
    case_status_div = div()
    all_num = 0
    pass_num = 0
    fail_num = 0
    bundle_cash_num = {}
    bundle_pass_num = {}
    bundle_pass_status = {}

    #bundle_count = report_case.getStandardtBundleCount()
    #standard_bundle = report_case.standard_bundle_list
    for bundle_index in report_case.result_bundle_list:  
        assert isinstance(bundle_index, int) 
        assert bundle_index > 0
        result_bundle = report_case.getResultBundle(bundle_index)
        bundle_cash_num[bundle_index] = len(result_bundle) 
        bundle_pass_num[bundle_index] = 0
        for key in result_bundle:
            all_num = all_num + 1
            if result_bundle[key]:
                pass_num = pass_num + 1
                bundle_pass_num[bundle_index] = bundle_pass_num[bundle_index] + 1
            else:
                fail_num = fail_num + 1                
        if bundle_cash_num[bundle_index] == bundle_pass_num[bundle_index]:
            bundle_pass_status[bundle_index] = True
        else:
            bundle_pass_status[bundle_index] = False
    #for index in range(0, report_case.getLength()):
    #    if report_case.getResult(index):
    #        pass_num = pass_num + 1
    #    else:
    #        fail_num = fail_num + 1

    case_status_div << span("Test Result:") 
    if fail_num == 0:
        case_status_div << span("Passed",style="color:green") << br()
    else:
        status_str = "Failed"
        case_status_div << span("Failed", style="color:red") << br()
    page << case_status_div
  
    page << a(name = 'h_summary')        
    page << div(b('Summary'), id='myCSSid1')

    version_div = div()
    for key in case.version_key:
        version_div << span("%s: %s"%(key,report_case.getVersionAttribute(key))) << br()
    page << version_div 


    summary_div = div()
    summary_div << span("Total cases:%d"%all_num) << br()
    summary_div << span("Passed cases:%d"%pass_num) << br()
    if fail_num != 0:
        summary_div << span("Failed cases:%d"%fail_num, style="color:red" ) << br()
    page << summary_div
    
    span_div = div(style = "border:1px ;height:20px")
    page << span_div
    if fail_num != 0:
        page << a("Failed summary:") 
        tableSummary = page << table()
        tablecss(tableSummary)
        titleTrSummary = tableSummary << tr()
        titleTrSummary << td('index', bgcolor=table_head_color_red, align="center")
        titleTrSummary << td('status', bgcolor=table_head_color_red, align="center")
        titleTrSummary << td('result', bgcolor=table_head_color_red, align="center")
        for key in cash.attribute_key:
            titleTrSummary << td(key, bgcolor=table_head_color_red,align="center")
        titleTrSummary << tr() <<tr()

        valueTrSummary = tableSummary << tr() 
        for bundle_index in report_case.result_bundle_list:  
            assert isinstance(bundle_index, int) 
            assert bundle_index > 0
            #for index in range(0,report_case.getLength()):  
            if bundle_pass_status[bundle_index]:
                valueTrSummary << td(colspan="9", align="center", bgcolor="green") << a("bundle %d"%bundle_index, href = "#h_bundle%d"%bundle_index) << tr() <<tr()
                continue
            else:
                valueTrSummary << td(colspan="9", align="center", bgcolor="red") << a("bundle %d"%bundle_index, href = "#h_bundle%d"%bundle_index) << tr()           
            
            for cash_index in report_case.result_bundle_list[bundle_index]:
                assert isinstance(cash_index, int) 
                assert cash_index > 0
                if report_case.result_bundle_list[bundle_index][cash_index]:
                    continue
                standard_cash = report_case.standard_bundle_list[bundle_index][cash_index]
                result_cash = report_case.run_bundle_list[bundle_index][cash_index]
                
                valueTrSummary << td(rowspan="2", align="center") << a('%d'%cash_index, href = "#h_bundle%d_case%d"%(bundle_index, cash_index))
                #if report_case.getResult(index):
                if report_case.result_bundle_list[bundle_index][cash_index]:
                    valueTrSummary << td(rowspan="2", align="center", bgcolor="green") << a('%s'%"pass") 
                else:
                    valueTrSummary << td(rowspan="2", align="center", bgcolor="red") << a('%s'%"failed") 
        
                valueTrSummary << td('%s'%"GOLDEN", bgcolor="grey",align="center")

                for key in cash.attribute_key:
                    valueTrSummary << td("%s"%standard_cash.getCashAttribute(key), bgcolor="grey", align="center")
                valueTrSummary = tableSummary << tr()
                valueTrSummary << td('%s'%"RUN", align="center")
                for key in cash.attribute_key:
                    valueTrSummary << td("%s"%result_cash.getCashAttribute(key), align="center")

                valueTrSummary = tableSummary << tr()  
    span_div = div(style = "border:1px ;height:20px")
    page << span_div

    if pass_num != 0:
        page << a("Pass summary:") 
        tableSummary = page << table()
        tablecss(tableSummary)
        titleTrSummary = tableSummary << tr()
        titleTrSummary << td('index', bgcolor=table_head_color_green, align="center")
        titleTrSummary << td('status', bgcolor=table_head_color_green, align="center")
        titleTrSummary << td('result', bgcolor=table_head_color_green, align="center")
        for key in cash.attribute_key:
            titleTrSummary << td(key, bgcolor=table_head_color_green,align="center")
        titleTrSummary << tr() <<tr()

        valueTrSummary = tableSummary << tr() 
        for bundle_index in report_case.result_bundle_list:  
            assert isinstance(bundle_index, int) 
            assert bundle_index > 0
            #for index in range(0,report_case.getLength()):  
            if bundle_pass_status[bundle_index]:
                valueTrSummary << td(colspan="9", align="center", bgcolor="green") << a("bundle %d"%bundle_index, href = "#h_bundle%d"%bundle_index) << tr() <<tr()
                continue
            else:
                valueTrSummary << td(colspan="9", align="center", bgcolor="red") << a("bundle %d"%bundle_index, href = "#h_bundle%d"%bundle_index) << tr()           
            
            for cash_index in report_case.result_bundle_list[bundle_index]:
                assert isinstance(cash_index, int) 
                assert cash_index > 0
                if not report_case.result_bundle_list[bundle_index][cash_index]:
                    continue
                standard_cash = report_case.standard_bundle_list[bundle_index][cash_index]
                result_cash = report_case.run_bundle_list[bundle_index][cash_index]
                
                valueTrSummary << td(rowspan="2", align="center") << a('%d'%cash_index, href = "#h_bundle%d_case%d"%(bundle_index, cash_index))
                #if report_case.getResult(index):
                if report_case.result_bundle_list[bundle_index][cash_index]:
                    valueTrSummary << td(rowspan="2", align="center", bgcolor="green") << a('%s'%"pass") 
                else:
                    valueTrSummary << td(rowspan="2", align="center", bgcolor="red") << a('%s'%"failed") 
        
                valueTrSummary << td('%s'%"GOLDEN", bgcolor="grey",align="center")

                for key in cash.attribute_key:
                    valueTrSummary << td("%s"%standard_cash.getCashAttribute(key), bgcolor="grey", align="center")
                valueTrSummary = tableSummary << tr()
                valueTrSummary << td('%s'%"RUN", align="center")
                for key in cash.attribute_key:
                    valueTrSummary << td("%s"%result_cash.getCashAttribute(key), align="center")

                valueTrSummary = tableSummary << tr()  
    span_div = div(style = "border:1px ;height:20px")
    page << span_div


   
   
    #if pass_num != 0:
    #    page << a("Passed summary:") 
    #    tableSummary = page << table()
    #    tablecss(tableSummary)
    #    titleTrSummary = tableSummary << tr()
    #    titleTrSummary << td('index', bgcolor=table_head_color_green, align="center")
    #    titleTrSummary << td('status', bgcolor=table_head_color_green, align="center")
    #    titleTrSummary << td('result', bgcolor=table_head_color_green, align="center")
    #    for key in cash.attribute_key:
    #        titleTrSummary << td(key, bgcolor=table_head_color_green,align="center")
    #    for index in range(0,report_case.getLength()):  
    #        if not report_case.getResult(index):
    #            continue
    #        standard_cash = report_case.getStdCash(index)
    #        result_cash = report_case.getResCash(index)
    #        valueTrSummary = tableSummary << tr() 
    #        valueTrSummary << td(rowspan="2", align="center") << a('%d'%index, href = "#h_case%d"%index)
    #        if report_case.getResult(index):
    #            valueTrSummary << td(rowspan="2", align="center", bgcolor="green") << a('%s'%"pass") 
    #        else:
    #            valueTrSummary << td(rowspan="2", align="center", bgcolor="red") << a('%s'%"failed") 
        
    #        valueTrSummary << td('%s'%"GOLDEN", bgcolor="grey",align="center")

    #        for key in cash.attribute_key:
    #            valueTrSummary << td("%s"%standard_cash.getCashAttribute(key), bgcolor="grey", align="center")
    #        valueTrSummary = tableSummary << tr()
    #        valueTrSummary << td('%s'%"RUN", align="center")
    #        for key in cash.attribute_key:
    #            valueTrSummary << td("%s"%result_cash.getCashAttribute(key), align="center")

    #        valueTrSummary = tableSummary << tr()  
            
    page << a(name = 'h_Detail')        
    page << div(b('Detail'), id='myCSSid1')

    for bundle_index in report_case.result_bundle_list:  
        assert isinstance(bundle_index, int) 
        assert bundle_index > 0

        if bundle_pass_status[bundle_index]:
            head_color = table_head_color_green
            #titleDiv = div(b('Bundle %d  Passed'%(bundle_index)), id='myCSSid1', style="color:%s"%head_color)
        else:
            head_color = table_head_color_red
            #titleDiv = div(b('Bundle %d Failed'%(bundle_index)), id='myCSSid1', style="color:%s"%head_color)  
        #page << titleDiv

        setup_list = report_case.standard_bundle_list[bundle_index][case.setup_str]
        tearoff_list = report_case.standard_bundle_list[bundle_index][case.tearoff_str]

        page << a(name = 'h_bundle%d'%(bundle_index))
        tableBundleDetail = page << table()
        tablecss(tableBundleDetail)
        bundleTrDetail = tableBundleDetail << tr()
        bundleTrDetail << td(colspan="2", bgcolor=head_color, align="center") << a("bundle %d"%bundle_index)
        bundleTrDetail << tr()
        bundleTrDetail << td('item', bgcolor=head_color, align="center") 
        bundleTrDetail << td('content', bgcolor=head_color, align="center") 
        bundleTrDetail << tr()
        bundleTrDetail << td(align="center") << a("cash number")
        bundleTrDetail << td(align="center") << a("%d"%bundle_cash_num[bundle_index])
        bundleTrDetail << tr()
        bundleTrDetail << td(align="center") << a("pass number")
        bundleTrDetail << td(align="center") << a("%d"%bundle_pass_num[bundle_index])
        bundleTrDetail << tr()
        bundleTrDetail << td(align="center") << a("fail number")
        bundleTrDetail << td(align="center") << a("%d"%(bundle_cash_num[bundle_index] - bundle_pass_num[bundle_index]))
        bundleTrDetail << tr()
        #bundleTrDetail << td(rowspan="%d"%len(setup_list), align="center") << a("setup commands")
        bundleTrDetail << td(rowspan="2", align="center") << a("setup commands")        
        for cmd in setup_list:
            bundleTrDetail << td(align="center") << a("%s"%cmd)
            bundleTrDetail = tableBundleDetail << tr()
            #bundleTrDetail << tr()
        bundleTrDetail << td(rowspan="%d"%len(tearoff_list), align="center") << a("tearoff commands")
        for cmd in tearoff_list:
            bundleTrDetail << td(align="center") << a("%s"%cmd)
            bundleTrDetail = tableBundleDetail << tr()
            #bundleTrDetail << tr()


        span_div = div(style = "border:1px ;height:20px")
        page << span_div
        page << tr()  

        titleTrDetail = tableBundleDetail << tr()
        
        for cash_index in report_case.result_bundle_list[bundle_index]:
            assert isinstance(cash_index, int) 
            assert cash_index > 0
            page << a(name = 'h_bundle%d_case%d'%(bundle_index, cash_index))
            if report_case.result_bundle_list[bundle_index][cash_index]:
                head_color = table_head_color_green
                titleDiv = div(b('Bundle %d Case %d Passed'%(bundle_index, cash_index)), id='myCSSid1', style="color:%s"%head_color)
            else:
                head_color = table_head_color_red
                titleDiv = div(b('Bundle %d Case %d Failed'%(bundle_index, cash_index)), id='myCSSid1', style="color:%s"%head_color)  
            page << titleDiv
           
            standard_cash = report_case.standard_bundle_list[bundle_index][cash_index]
            run_cash = report_case.run_bundle_list[bundle_index][cash_index]
            
            tableDetail = page << table()
            tablecss(tableDetail)
            titleTrDetail = tableDetail << tr()
            titleTrDetail << td('item', bgcolor=head_color, align="center")
            titleTrDetail << td('GOLDEN', bgcolor=head_color, align="center")
            titleTrDetail << td('RUN', bgcolor=head_color, align="center")
            valueTrDetail = tableDetail << tr()
            for key in cash.attribute_key:
                valueTrDetail << td("%s"%key, align="center")
                valueTrDetail << td("%s"%standard_cash.getCashAttribute(key), align="center")
                valueTrDetail << td("%s"%run_cash.getCashAttribute(key), align="center")
                valueTrDetail = tableDetail << tr() 
            for key in cash.file_path_key:
                 valueTrDetail << td("%s"%key, align="center")
                 valueTrDetail << td(colspan="2", align="center") << a("%s"%standard_cash.geFileAttribute(key))
                 valueTrDetail = tableDetail << tr()
            page << a('Return to Summary', href = "#h_summary") 
            span_div = div(style = "border:1px ;height:50px")
            page << span_div
            page << tr()    
    #for index in range(0,report_case.getLength()):
    #    standard_cash = report_case.getStdCash(index)
    #    result_cash = report_case.getResCash(index)
    #    page << a(name = 'h_case%d'%index)

    #    if report_case.getResult(index):
    #        head_color = table_head_color_green
    #        titleDiv = div(b('Case %d Passed'%index), id='myCSSid1', style="color:%s"%head_color)
    #    else:
    #        head_color = table_head_color_red
    #        titleDiv = div(b('Case %d Failed'%index), id='myCSSid1', style="color:%s"%head_color)        
    #    page << titleDiv

    #    tableDetail = page << table()
    #    tablecss(tableDetail)
    #    titleTrDetail = tableDetail << tr()
        
    #    titleTrDetail << td('item', bgcolor=head_color, align="center")
    #    titleTrDetail << td('GOLDEN', bgcolor=head_color, align="center")
    #    titleTrDetail << td('RUN', bgcolor=head_color, align="center")
    #    valueTrDetail = tableDetail << tr()
    #    for key in cash.attribute_key:
    #        valueTrDetail << td("%s"%key, align="center")
    #        valueTrDetail << td("%s"%standard_cash.getCashAttribute(key), align="center")
    #        valueTrDetail << td("%s"%result_cash.getCashAttribute(key), align="center")
    #        valueTrDetail = tableDetail << tr() 
    #    for key in cash.file_path_key:
    #        valueTrDetail << td("%s"%key, align="center")
    #        valueTrDetail << td(colspan="2", align="center") << a("%s"%standard_cash.geFileAttribute(key))
    #        valueTrDetail = tableDetail << tr()
        #page << a('Return to Summary', href = "#h_summary") 
        #span_div = div(style = "border:1px ;height:50px")
        #page << span_div

        #page << tr()    

    page.printOut(fileName)